package cn.edu.hznu.coursework.controller;

import java.util.Optional;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.hznu.coursework.entity.Student;
import cn.edu.hznu.coursework.entity.Teacher;
import cn.edu.hznu.coursework.entity.UserRole;
import cn.edu.hznu.coursework.service.SignService;

@Controller
public class HomeController {

	@Autowired
	private SignService signService;

	@RequestMapping(value = "/")
	public String getIndex(HttpSession session) {
		Object user = session.getAttribute("user");
		if(user == null) {
			return "index";
		} else {
			if(Teacher.class.isInstance(user)) {
				return "redirect:/teacher";
			} else if(Student.class.isInstance(user)) {
				return "redirect:/student";
			} else {
				return "index";
			}
		}
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String getLogin() {
		return "index";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String logn(
			@RequestParam(value = "no", required = true) String userNo,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "role", required = true) String role,
			ModelMap model, HttpSession session) {
		
		switch (UserRole.valueOf(role.toUpperCase())) {
			case STUDENT:
				Optional<Student> student = Optional.ofNullable(this
						.signService.studentLogin(userNo, password));
				if (student.isPresent()) {
					session.setAttribute("user", student.get());
					return "redirect:/student";
				} else {
					model.addAttribute("error", "user ID or password is error.");
					return "index";
				}
			case TEACHER:
				Optional<Teacher> teacher = Optional.ofNullable(this
						.signService.teacherLogin(userNo, password));
				if (teacher.isPresent()) {
					session.setAttribute("user", teacher.get());
					return "redirect:/teacher";
				} else {
					model.addAttribute("error", "user ID or password is error.");
					return "index";
				}
			default:
				return "index";
		}
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(
			@RequestParam(value = "no", required = true) String userNo,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "role", required = true) String role,
			ModelMap model) {
		
		switch (UserRole.valueOf(role.toUpperCase())) {
			case STUDENT:
				Student student = new Student();
				student.setNo(userNo);
				student.setPassword(password);
				student.setName(name);
				try {
					this.signService.studentRegister(student);
					model.addAttribute("info", "注册成功，请登录");
					return "index";
						
				} catch (Exception e) {
					model.addAttribute("error", "注册失败, " + e.getMessage());
					return "index";
				}
			case TEACHER:
				Teacher teacher = new Teacher();
				teacher.setNo(userNo);
				teacher.setPassword(password);
				teacher.setName(name);
				try {
					this.signService.teacherRegister(teacher);
					model.addAttribute("info", "注册成功，请登录");
					return "index";
						
				} catch (Exception e) {
					model.addAttribute("error", "注册失败, " + e.getMessage());
					return "index";
				}
			default:
				return "index";
		}
		
	}
	
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session, HttpServletResponse response){
		session.invalidate();
		return "redirect:/";
	}
}
